function nowDate() {
  var date = new Date();
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var day = date.getDate();
  var hour = date.getHours();
  var minute = date.getMinutes();
  var second = date.getSeconds();
  return (
    year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second
  );
}

/**
 * 字段转化为驼峰格式
 * @param {*} str
 * @returns
 */
function toCamelCase(str) {
  if (str == null || str == "" || str == undefined) {
    return str;
  }
  let arr = str.split("_");
  let result = "";
  arr.forEach((ele) => {
    for (let i = 0; i < ele.length; i++) {
      let item = ele[i];
      if (i == 0) {
        item = ele[i].toUpperCase();
      }
      result = result + item;
    }
  });
  return result;
}

/**
 * 字段转化
 * @param {*} str
 * @returns
 */
function toFieldCase(str) {
  var camel = toCamelCase(str);
  return camel.charAt(0).toLowerCase() + camel.slice(1);
}

/**
 * 去掉后缀
 * @param {*} str
 * @param {*} suffix
 * @returns
 */
function removeSuffix(str, suffix) {
  if (str == null || str == "" || str == undefined) {
    return str;
  }
  if (suffix == null || suffix == "" || suffix == undefined) {
    return str;
  }

  if (str.endsWith(suffix)) {
    return str.substring(0, str.lastIndexOf(suffix));
  }

  return str;
}

/**
 * 去掉前缀
 * @param {*} str
 * @param {*} prefix
 * @returns
 */
function removePrefix(str, prefix) {
  if (str == null || str == "" || str == undefined) {
    return str;
  }
  if (prefix == null || prefix == "" || prefix == undefined) {
    return str;
  }

  if (str.startsWith(prefix)) {
    return str.substring(prefix.length, str.length);
  }

  return str;
}

/**
 * 清除前后缀
 * @param {*} str
 * @param {*} prefix
 * @param {*} suffix
 * @returns
 */
function purefix(str, prefix, suffix) {
  let sss = removeSuffix(str, suffix);

  let xxxx = removePrefix(sss, prefix);

  return xxxx;
}

const UTILS = {
  nowDate,
  toCamelCase,
  toFieldCase,
  removeSuffix,
  removePrefix,
  purefix,
};

export default UTILS;
